package com.aid.winter.service.impl;

import com.aid.winter.common.exception.BusinessException;
import com.aid.winter.dto.request.ClothesStyleCreateRequest;
import com.aid.winter.dto.response.ClothesStyleVO;
import com.aid.winter.dto.response.PageResult;
import com.aid.winter.entity.AidClothesStyle;
import com.aid.winter.mapper.AidClothesStyleMapper;
import com.aid.winter.service.ClothesStyleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class ClothesStyleServiceImpl implements ClothesStyleService {

    private final AidClothesStyleMapper styleMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ClothesStyleVO createStyle(ClothesStyleCreateRequest request) {
        AidClothesStyle style = new AidClothesStyle();
        BeanUtils.copyProperties(request, style);
        style.setStatus(1);
        
        styleMapper.insert(style);
        
        return convertToVO(style);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ClothesStyleVO updateStyle(Long id, ClothesStyleCreateRequest request) {
        AidClothesStyle style = getStyleById(id);
        
        BeanUtils.copyProperties(request, style);
        styleMapper.updateById(style);
        
        return convertToVO(style);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteStyle(Long id) {
        AidClothesStyle style = getStyleById(id);
        styleMapper.deleteById(id);
    }

    @Override
    public ClothesStyleVO getStyleDetail(Long id) {
        return convertToVO(getStyleById(id));
    }

    @Override
    public PageResult<ClothesStyleVO> listStyles(Integer gender, Integer status, Integer pageNum, Integer pageSize) {
        int offset = (pageNum - 1) * pageSize;
        
        List<AidClothesStyle> styles = styleMapper.selectList(gender, status, offset, pageSize);
        long total = styleMapper.selectCount(gender, status);
        
        List<ClothesStyleVO> voList = styles.stream()
                .map(this::convertToVO)
                .toList();
        
        return PageResult.build(voList, total, pageSize, pageNum);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(Long id, Integer status) {
        if (status != 0 && status != 1) {
            throw new BusinessException("无效的状态值");
        }
        
        AidClothesStyle style = getStyleById(id);
        style.setStatus(status);
        styleMapper.updateById(style);
    }

    private AidClothesStyle getStyleById(Long id) {
        AidClothesStyle style = styleMapper.selectById(id);
        if (style == null) {
            throw new BusinessException("款式不存在");
        }
        return style;
    }

    private ClothesStyleVO convertToVO(AidClothesStyle style) {
        ClothesStyleVO vo = new ClothesStyleVO();
        BeanUtils.copyProperties(style, vo);
        return vo;
    }
} 